package com.pb.camera.jph.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.pb.camera.R;
import com.pb.camera.bean.DeviceMode;
import com.pb.camera.view.ProgressDialog;
import com.pb.camera.work.dr_peng.ChannleWorkInterface;
import com.pb.camera.work.dr_peng.DrPengChannleWork;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class SettingCameraNetActivity extends Activity implements OnClickListener{
	private static final int SHOW_PROGRESS = 0;
	private static final int SHOW_TOAST = 1;
	private ImageButton ib_back_add;
	private TextView tv_cancel,tv_next;
	private ProgressDialog mProgressDialog;
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == SHOW_PROGRESS)
				mProgressDialog.show();
			if (msg.what == SHOW_TOAST)
				Toast.makeText(SettingCameraNetActivity.this,(String)msg.obj,Toast.LENGTH_SHORT).show();
		}
	};
	private DeviceMode mDeviceMode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_camera_net);
		initView();
		initData();
	}

	private void initView() {
		ib_back_add = (ImageButton) findViewById(R.id.ib_back_add);
		tv_cancel = (TextView) findViewById(R.id.tv_cancel);
		tv_next = (TextView) findViewById(R.id.tv_next);

	}

	private void initData() {
		mProgressDialog = new ProgressDialog(this,false);
		ib_back_add.setOnClickListener(this);
		tv_cancel.setOnClickListener(this);
		tv_next.setOnClickListener(this);
		Intent intent = getIntent();
		mDeviceMode = (DeviceMode) intent.getSerializableExtra("device");
		mProgressDialog.show();
		DrPengChannleWork.addDevice2Group(mDeviceMode, new ChannleWorkInterface<String>() {

			@Override
			public String onSuccess(String s) {
				s = getString(R.string.add_device_success);
				Message msg = new Message();
				msg.what = SHOW_TOAST;
				msg.obj = s;
				handler.sendMessage(msg);
				mProgressDialog.dismiss();
				return null;
			}

			@Override
			public void onFailed(String t) {
				if (t != null)
					t = getString(R.string.add_device_failed)+":"+t;
				Message msg = new Message();
				msg.what = SHOW_TOAST;
				msg.obj = t;
				handler.sendMessage(msg);
				mProgressDialog.dismiss();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_back_add:
			finish();
			break;
		case R.id.tv_cancel:
			finish();
			break;
		case R.id.tv_next:
			Intent intent = new Intent(this, SelectCameraActivity.class);
			intent.putExtra("UUID",mDeviceMode.getDevid());
			this.startActivity(intent);
			break;
		}
	}
}
